package org.spring.springboot.swagger.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.swagger.bean.Demo;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
@Api(value="测试swagger接口", tags="接口")
@RestController
public class HelloSwaggerController {
	private Logger logger = LoggerFactory.getLogger(HelloSwaggerController.class);

    /** * 可以直接使用@ResponseBody响应JSON 
     * @param request  
     * @param response
     * @return 
     * */
    @ResponseBody
    @RequestMapping(value = "/getcount", method = RequestMethod.POST)
    @ApiOperation(value="方法简要说明", notes="方法及业务详细说明")
    public ModelMap getCount() {
        logger.info(">>>>>>>> begin getCount >>>>>>>>");
        ModelMap map = new ModelMap();
        map.addAttribute("count", 158);

        // 后台获取的国际化信息
        map.addAttribute("xstest", "测试");
        return map;
    }

    /** * 可以直接使用@ResponseBody响应JSON * * @param request * @param response * @return */
    @ApiIgnore//使用该注解忽略这个API
    @ResponseBody
    @RequestMapping(value = "/jsonTest1", method = RequestMethod.POST)
    public ModelMap jsonTest() {
        ModelMap map = new ModelMap();
        map.addAttribute("hello", "你好");
        map.addAttribute("veryGood", "很好");

        return map;
    }

    /** 
     *  可以直接使用@ResponseBody响应JSON
     *  @param request 
     *  @param response 
     *  @return 
     */
    @ApiOperation(value="方法简要说明", notes="方法及业务详细说明")
    @ResponseBody
    @RequestMapping(value = "/jsonTest3", method = RequestMethod.POST)
    public List<String> jsonTest3() {
        List<String> list = new ArrayList<String>();
        list.add("hello");
        list.add("你好");
        return list;
    }

    /** * JSON请求一个对象<br/>
     * （Ajax Post Data：{"name":"名称","content":"内容"}）
     * @param version * 
     * @return 
     * */
    @ApiOperation(value="方法简要说明2", notes="方法及业务详细说明2")
    @ResponseBody
    @RequestMapping(value = "/jsonTest2", method = RequestMethod.POST)
    public ModelMap jsonTest2(@RequestBody Demo demo) {
        logger.info("demoName：" + demo.getName());
        logger.info("demoContent：" + demo.getContent());
        ModelMap map = new ModelMap();
        map.addAttribute("result", "ok");
        return map;
    }

    /** 直接读取URL参数值<br/>
     * /jsonTest6.do?name=Hello&content=World 
     * @param demoName 
     * @param content 
     * @return 
     */
    @ResponseBody
    @RequestMapping(value = "/jsonTest6", method = RequestMethod.POST)
    @ApiOperation(value="方法简要说明6", notes="方法及业务详细说明6")
    @ApiImplicitParams({
    		@ApiImplicitParam(name="demoName",value="名称",dataType="String"),
    		@ApiImplicitParam(name="content",value="内容",dataType="String")})
    public ModelMap jsonTest6(@RequestParam("name") String demoName, @RequestParam String content) {
        logger.info("demoName：" + demoName);
        ModelMap map = new ModelMap();
        map.addAttribute("name",demoName + "AAA");
        map.addAttribute("content",content + "BBB");
        map.addAttribute("date",new java.util.Date());
        return map;
    }

    /** * JSON请求一个对象，将RequestBody自动转换为JSONObject对象<br/> 
     * （Ajax Post Data：{"name":"名称","content":"内容"}） 
     * 使用JSONObject请添加依赖
     * @param version
     * @return 
     */
    @ApiOperation(value="方法简要说明5", notes="方法及业务详细说明5")
    @ResponseBody
    @RequestMapping(value = "/jsonTest5", method = RequestMethod.POST)
    public ModelMap jsonTest5(@RequestBody JSONObject jsonObject) {
        String name = jsonObject.getString("name");
        logger.info("demoName：" + name);
        ModelMap map = new ModelMap();
        map.addAttribute("demoName",name);
        return map;
    }

    /**
     * 输入 和输出为JSON格式的数据的方式 HttpEntity<?> ResponseEntity<?>
     * @param u 
     * @return 
     */
    @ResponseBody
    @RequestMapping(value = "/jsonTest4", method = RequestMethod.POST)
    @ApiOperation(value="方法简要说明4", notes="方法及业务详细说明4")
    public ResponseEntity<String> jsonTest4(HttpEntity<Demo> demo) {
        //获取Headers方法
        HttpHeaders headers = demo.getHeaders();
        MediaType t = headers.getContentType();

        logger.info(t.toString());
        // 获取内容
        String demoContent = demo.getBody().getContent();

        // 这里直接new一个对象（HttpHeaders headers = new HttpHeaders();）
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("MyHeaderName", "SHANHY");

        ResponseEntity<String> responseResult = new ResponseEntity<String>(
                demoContent, responseHeaders, HttpStatus.OK);
        return responseResult;
    }

}
